import Vue from 'vue'
import Router from 'vue-router'
import layout from '@/page/layout/layout'

Vue.use(Router)


// 任何角色都能访问路由表
export const syncRouterMap = [
  {
    path: '/',
    component: () => import('@/page/login'),
    hidden: true
  },
  {
    path: '/index',
    component: layout,
    meta: {
      name: '系统主页',
      icon: 'el-icon-tableware'
    },
    children: [
      {
        path: '',
        component: () => import('@/page/system/index'),
        hidden: false
      }
    ],
    hidden: false
  }
]
const router = new Router({
  mode: 'history',
  routes: syncRouterMap
}) 

function generaChildrenMenu(data){
  let menuChildren = [];
  if(data.children.length==0){
    const menu = {
      path:'',
      component: () => import(`@/page/system${data.path}/index.vue`),
    }
    menuChildren.push(menu);
    return menuChildren;
  }
  data.children.forEach(item => {
    const menuCn = {
      path: item.path,
      component: () => import(`@/page/system/${item.path}/index.vue`),
      meta: item.meta,
      hidden: false
    }
    menuChildren.push(menuCn);
  })
  return menuChildren;
}
export function generateMenu(data) {
  let menus = [];
  data.forEach(item => {
    const menu = {
      path: item.path === '#' ? '' : item.path,
      component: layout,
      children: [],
      meta: item.meta,
      id: item.id,
      hidden: false
    }
    menu.children=generaChildrenMenu(item);
    menus.push(menu);
    router.addRoute(menu);
  })
  return menus;
}
export default router